using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Universe.Farm.Service.Mesages
{
    static class ScopeSerializer
    {
        public static ServicesScope Read(BinaryReader reader)
        {
            ServicesScope scope = new ServicesScope();
            scope.IsFullScope = reader.ReadBoolean();
            if (!scope.IsFullScope)
            {
                int n = reader.ReadInt32();
                for (int i = 0; i < n; i++)
                {
                    string computerName = reader.ReadBoolean() ? reader.ReadString() : null;
                    string serviceName = reader.ReadBoolean() ? reader.ReadString() : null;
                    scope.Add(computerName, serviceName);
                }
            }

            return scope;
        }
        
        public static void Write(ServicesScope scope, BinaryWriter writer)
        {
            writer.Write(scope.IsFullScope);
            if (!scope.IsFullScope)
            {
                writer.Write(scope.Items.Count);
                foreach (ServicesScope.Item item in scope.Items)
                {
                    writer.Write(item.Computer != null);

                    if (item.Computer != null)
                        writer.Write(item.Computer);

                    writer.Write(item.ServiceName != null);

                    if (item.ServiceName != null)
                        writer.Write(item.ServiceName);

                }
            }
            
        }
    }
}
